package textanalyzer;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Map.Entry;


public class MapSorter {
	HashMap<String, Integer> unsortedMap; 
	
	public MapSorter(HashMap<String, Integer> input) {
		unsortedMap = input;
	}
	
	public LinkedHashMap<String, Integer> sortMap() {
		LinkedHashMap<String, Integer> output= new LinkedHashMap<String, Integer>();
		while(!unsortedMap.isEmpty()){
			Iterator<Entry<String, Integer>> iter = unsortedMap.entrySet().iterator();
			String maxKey = iter.next().getKey();
			while (iter.hasNext()) {
				String tmpKey = iter.next().getKey();
				if(unsortedMap.get(tmpKey)>unsortedMap.get(maxKey))maxKey = tmpKey;				
			}
			output.put(maxKey, unsortedMap.get(maxKey));
			unsortedMap.remove(maxKey);
		}
				
		return output;
	}
}
